package org.yfl.mbg.enhance.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.yfl.mbg.enhance.utils.ClassUtil;

import java.util.List;

/**
 * 生成的类添加注释插件
 * @author yanfeilin
 * @version 1.0.0
 * @date 2020/5/25 22:30
 */
public class JavaDocPlugin extends PluginAdapter {

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        addJavaDoc(introspectedTable,topLevelClass);
        return true;
    }



    private void addJavaDoc(IntrospectedTable introspectedTable,TopLevelClass topLevelClass){
        //添加注释
        String tableRemarks = introspectedTable.getRemarks();
        String domainJavaDoc = ClassUtil.getClassJavaDoc(tableRemarks);
        topLevelClass.addJavaDocLine(domainJavaDoc);
    }

    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
        //添加注释
        String tableRemarks = introspectedTable.getRemarks()+"dao";
        String domainJavaDoc = ClassUtil.getClassJavaDoc(tableRemarks);
        interfaze.addJavaDocLine(domainJavaDoc);
        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        //新增表格字段注释
        List<String> javaDocLines = field.getJavaDocLines();
        javaDocLines.clear();
        String javaDoc = ClassUtil.getClassFieldJavaDoc(introspectedColumn.getRemarks());
        field.addJavaDocLine(javaDoc);
        return true;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientBasicInsertMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientBasicInsertMultipleMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientBasicDeleteMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientBasicInsertMultipleHelperMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientBasicSelectOneMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientBasicUpdateMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientInsertSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean clientInsertMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();

        return true;
    }

    @Override
    public boolean clientInsertMultipleMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        method.getJavaDocLines().clear();
        return super.clientInsertMultipleMethodGenerated(method, interfaze, introspectedTable);
    }
}
